package com.example.a1.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.a1.R;
import com.example.a1.model.Course;
import com.example.a1.util.GlideUtil;
import com.example.a1.util.HttpUtil;

public class CourseDetailActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_detail);

        // 初始化控件
        ImageView imageIv = findViewById(R.id.course_detail_image);
        TextView titleTv = findViewById(R.id.course_detail_title);
        TextView teacherTv = findViewById(R.id.course_detail_teacher);
        TextView difficultyTv = findViewById(R.id.course_detail_difficulty);
        TextView ratingTv = findViewById(R.id.course_detail_rating);
        TextView descTv = findViewById(R.id.course_detail_description);

        // 获取传递的参数
        String detailUrl = getIntent().getStringExtra("course_detail_url");

        // 加载课程详情
        HttpUtil.getCourseDetail(detailUrl, new HttpUtil.OnDataLoadedListener<Course>() {
            @Override
            public void onSuccess(Course course) {
                titleTv.setText(course.getTitle());
                teacherTv.setText("讲师: " + course.getTeacher());
                difficultyTv.setText("难度: " + course.getDifficulty());
                ratingTv.setText(String.format("评分: %.1f", course.getRating()));
                descTv.setText("课程详细介绍：\n本课程将系统讲解" + course.getTitle() + "的核心知识，适合" + course.getDifficulty() + "学习者。");
                GlideUtil.loadImage(CourseDetailActivity.this, course.getImageUrl(), imageIv);
            }

            @Override
            public void onFailure(String error) {
                Toast.makeText(CourseDetailActivity.this, "详情加载失败: " + error, Toast.LENGTH_SHORT).show();
            }
        });
    }
}